package de.preissler.selection;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.HSuperColumn;
import me.prettyprint.hector.api.beans.SuperRow;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.query.RangeSuperSlicesQuery;

public class WinnersView implements Winners
{
	private static final String SCF = "Quotes_by_Change";

	public Map<String, List<ChangeOfISIN>> getWinners(List<String> dates, Keyspace keyspace,
			Map<String, List<ChangeOfISIN>> result, int number)
	{
		//Daten aus der Datenbank holen
		RangeSuperSlicesQuery<String, Long, String, String> query = HFactory.createRangeSuperSlicesQuery(keyspace, SS,
				LS,	SS, SS);
		query.setColumnFamily(SCF);
		query.setKeys(dates.get(dates.size()-1), dates.get(0));
		query.setRange(null, null, true, number);
		List<SuperRow<String, Long, String, String>>  queryResult = query.execute().get().getList();
		
		//Ergebnis in das Endformat konvertieren
		for(SuperRow<String, Long, String, String> sr : queryResult)
		{
			List<ChangeOfISIN> listOfCOI = new LinkedList<ChangeOfISIN>();
			for(HSuperColumn<Long, String, String> sc : sr.getSuperSlice().getSuperColumns())
			{
				List<String> isins = new LinkedList<String>();
				for(HColumn<String, String> c : sc.getColumns())
				{
					isins.add(c.getName());
				}
				listOfCOI.add(new ChangeOfISIN(sc.getName()/1000000.0, isins));
			}
			result.put(sr.getKey(), listOfCOI);
		}
		
		return result;
	}

}
